import { Component, OnInit, ViewChild, Output, Input, EventEmitter } from '@angular/core';
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
import { NzModalRef, NzFormatEmitEvent, NzModalService, NzTreeComponent, NzTreeNodeOptions, NzMessageService } from 'ng-zorro-antd';
import { PagesService } from 'services/pages.service';
import { UtilService } from 'services/util.service';
import { RequestService } from 'services/request.service';
declare var layui: any;
declare var layer: any;
declare var $: any;

@Component({
  selector: 'app-more',
  templateUrl: './more.component.html',
  styleUrls: ['./more.component.scss']
})
export class MoreComponent implements OnInit {

  constructor(public router: Router, private http: HttpClient, public interfaceService: PagesService, public utilService: UtilService, public request: RequestService) {

  }

  @Output() private outer = new EventEmitter();
  @Input() one: any;//父级元素指针

  ngOnInit() {

  }

  field1 = []     //table表格1-主表列字段集合
  data1 = {}      //table表格1-主表对象数据集合
  data1_dis = {}  //table表格1-主表对象禁用集合
  data1_sel = []  //table表格1-主表下拉框数据集合



  popup11_url = '';
  popup11_unitId = '';
  popup11_check = 0
  getfieldsbytypes = '';
  getfieldsbytype = '';
  getfieldsbyoption = '';
  suppliercode = ""
  /**
   * 
   * @param url 动态接口
   * @param unitId 被操作模块的实时属主
   * @param radio 可选参数，单选0或多选1，不传默认单选0
   * getfieldsbytypes 去除带.的动态列type值（table表格中带点字符串无法识别）
   * getfieldsbytype  动态列type值
   * getfieldsbyoption  下拉接口地址
   */
  popup11_show(url, unitId, radio?): void {
    this.field1 = []
    this.popup11_url = url
    this.popup11_unitId = unitId
    this.getfieldsbytypes = this.one.filedRun().split(".").join("");
    // if(typeof(this.one.parameter_ass()!=undefined)){
    //   this.suppliercode = this.one.parameter_ass()
    // }
    // this.data1['vdrWarehCode'] = this.suppliercode
    this.getfieldsbytype = this.one.filedRun();
    this.getfieldsbyoption = this.one.optionRun();
    this.data1[this.getfieldsbytype] = true;
    this.interfaceService.postPage("/erp/common/fields/getfieldsbytype", { "type": this.getfieldsbytype }).subscribe((data: any) => {
      if (this.interfaceService.codeValidation(data)) {
      if (data.code == 200) {
        if (typeof (radio) == "undefined") {
          this.popup11_check = 0
          this.field1 = this.utilService.filed_list(data.object);
        } else {
          this.popup11_check = 1
          this.field1 = this.utilService.filed_list(data.object, 1);
        }

        for (var i = 0; i < data.object.length; i++) {
          this.data1[data.object[i]["fieldValue"]] = ""
        }
        this.popup11_table(this.getfieldsbytype);
        if (this.getfieldsbyoption !== "") {
          this.interfaceService.postPage(this.getfieldsbyoption, {}).subscribe((data2: any) => {
            if (this.interfaceService.codeValidation(data2)) {
            if (data2.code == 200) {
              for (let i in data2.object) {
                this.data1[i] = null
                this.data1_sel[i] = data2.object[i]
              }
            }
          }
          });
        }
      }
    }
    });


  }
  popup11_hide(): void {
    this.data1[this.getfieldsbytype] = false;
    for (let i in this.data1) {
      if (i !== this.getfieldsbytype) {
        this.data1[i] = ""
      }
    }
  }

  /**
   * 动态表格
   * @param type 动态列类型
   * payment.PprByPucVo 关联采购合同
   * payment.PmnVo      关联预付款单
   * warehouse.pprGrnVO 关联入库单
   */
  popup11_table(type) {
    this.data1['startTime'] = typeof (this.data1['startTime']) == "undefined" ? "" : this.data1['startTime']
    this.data1['endTime'] = typeof (this.data1['endTime']) == "undefined" ? "" : this.data1['endTime']
    var para = ""
    if (type === 'payment.PprByPucVo') {//关联采购合同
      para = '&pucNum=' + this.data1['pucNum'] + '&vdrWarehCode=' + this.data1['vdrWarehCode'] + '&vdeWarehCode='
        + this.data1['vdeWarehCode'] + '&startTime=' + this.data1['startTime'] + '&endTime=' + this.data1['endTime']
    } else if (type === 'payment.PmnVo') {//关联预付款单
      para = '&pmnNum=' + this.data1['pmnNum'] + '&vdeWarehCode='
        + this.data1['startTime'] + '&endTime=' + this.data1['endTime']
    } else if (type === 'warehouse.pprGrnVO') {//关联入库单
      para = '&grnNum=' + this.data1['grnNum'] + '&startTime=' + this.data1['startTime'] + '&endTime=' + this.data1['endTime']
    } else if (type === 'payment.PprVo') {//采购付款申请单
      para = '&pprNum=' + this.data1['pprNum'] + '&venderCode=' + this.data1['vdrWarehCode']
        + '&startTime=' + this.data1['startTime'] + '&endTime=' + this.data1['endTime']
    } else if (type === 'system.SysRoleVo') {//角色弹窗
      para = '&roleId=' + this.data1['roleId'] + '&description=' + this.data1['description']
    } else if (type === 'prdCycle.PrdCycleParamVo') {//商品周期动作参数配置弹窗
      para = '&actionId=' + this.data1['actionId'] + '&parameterName=' + this.data1['parameterName']
    } else if (type === 'shop.ShopEmpVo') {//导购
      para = ''
    } else if (type === 'system.SysPrivilegeVo') {//权限弹窗
      const sele1 = this.data1['privType'] == null ? '' : this.data1['privType']
      para = '&privId=' + this.data1['privId'] + '&description=' + this.data1['description'] + '&privType=' + sele1
    }
    this.listTable(this.popup11_url + para);
  }



  listTable(url): void {
    setTimeout(() => {
      layui.use(['table',  'laydate'], (obj) => {
        var  table = layui.table,  laydate = layui.laydate;
        laydate.render({
          elem: '#popup11_date1'
          , done: (value) => {
            this.data1['startTime'] = value;
          }
        });
        laydate.render({
          elem: '#popup11_date2'
          , done: (value) => {
            this.data1['endTime'] = value;
          }
        });


        //数据表格实例化			
        var tab = this.utilService.getTable(url, "#" + this.getfieldsbytypes, this.field1)
        tab["title"] = '数据表'
        if (this.popup11_check == 1) {
          tab["toolbar"] = "#" + this.getfieldsbytypes + "2"
          tab["defaultToolbar"] = []
        }
        table.render(tab);
        //监听行单击事件row   监听行双击事件rowDouble
        table.on('row(' + this.getfieldsbytypes + ')', (obj) => {
          obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');//标注选中样式
        });
        table.on('rowDouble(' + this.getfieldsbytypes + ')', (obj) => {
          if (this.popup11_check == 0) {//单选
            this.popup11_hide();
            this.outer.emit(obj.data)
          }
        });
        //头工具栏事件
        table.on('toolbar(' + this.getfieldsbytypes + ')', (obj) => {
          var checkStatus = table.checkStatus(obj.config.id);
          switch (obj.event) {
            case 'getCheckData':
              var data = checkStatus.data;
              // layer.alert(JSON.stringify(data));
              //layer.msg('选中了：'+ data.length + ' 个');
              if (data.length > 0) {
                this.popup11_hide();
                this.outer.emit(data)
              } else {
                layer.msg("请选择数据后操作！")
              }
              break;
          };
        });

      });

    }, 50);


  }




  //组织弹窗
  @ViewChild('unitChild') unit: any;
  popup12_id = 0
  popup12_url = ""
  popup12_uid = ""
  popup12_show(id, type, value?): void {//组织通用组件，value没有值为弹窗事件，value有值为数据验证事件（光标离开事件）
    this.popup12_id = id
    this.popup12_url = "&unitType=" + type + "&switchUnitId=" + this.popup11_unitId
    if (!this.utilService.patrn.test(value) || value == null || typeof (value) == "undefined") {
      this.unit.popup01_show("/erp/unit/option/unitOption?1=1" + this.popup12_url);
    } else {
      this.unit.popup001_show(this.popup11_unitId, type, value);
    }
  }

  unitParent(e) {		//接收子组件传递过来的数据
    e.unitId = e.unitId == null ? "" : e.unitId
    e.unitCode = e.unitCode == null ? "" : e.unitCode
    e.unitName = e.unitName == null ? "" : e.unitName
    if (this.getfieldsbytype == "payment.PprByPucVo") {
      if (this.popup12_id == 1) {//供应商仓库代码-搜索
        this.data1["vdrWarehCode"] = e.unitCode;
      } else if (this.popup12_id == 2) {//采购商仓库代码-搜索
        this.data1["vdeWarehCode"] = e.unitCode;
      }
    } else if (this.getfieldsbytype == "payment.PprVo") {
      if (this.popup12_id == 1) {//供应商仓库代码-搜索
        this.data1["vdrWarehCode"] = e.unitCode;
      }
    }
  }


}
